<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="租户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rentId">
              <a-select
                :show-search="true"
                :show-arrow="true"
                :allow-clear="true"
                :value="rentId"
                placeholder="请输入姓名或首字母"
                :filter-option="false"
                :not-found-content="null"
                @search="handleSearch"
                @change="handleChange"
              >
                <a-select-option v-for="d in data" :key="d.value">
                  {{ d.text }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="出租面积" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rentArea">
              <a-input-number v-model="model.rentArea" placeholder="请输入出租面积" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'VcaFarmlandRentForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{

        },
        rentId:undefined,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          rentArea: [
            { required: true, message: '请输入出租面积!'},
          ],
        },
        data: [],
        url: {
          add: "/villageCadreAssist/vcaFarmlandRent/add",
          edit: "/villageCadreAssist/vcaFarmlandRent/edit",
          queryById: "/villageCadreAssist/vcaFarmlandRent/queryById",
          queryTopN: "/villageCadreAssist/vcaFarmland/queryTopN"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    methods: {
      add (record) {
        this.edit({ownId:record["id"],rentArea:record["tillageArea"]});
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      handleSearch(value) {
        this.data.splice(0,this.data.length);
        if(value.length>=2){
          getAction(this.url.queryTopN,{firstLetter:value}).then((res) => {
            if (res.success) {
              if(res.result){
                //let departName = this.userInfo["orgCodeTxt"];
                res.result.forEach(i=>{
                  let address = i["groupName"]
                  this.data.push({
                    text:i["farmName"]+(address?" ("+address+")":""),
                    value:i["id"]
                  });
                });
              }
            }else{
              this.$message.warning(res.message);
            }
          });
        }
      },
      handleChange(value) {
        console.log("handleChange",value);

        this.rentId = value
        if(!value){
          this.rentId = null
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model["rentId"]=this.rentId;
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>